package setting

import (
	"fmt"
	"time"
	"github.com/micro/go-micro/v2/config"
	"github.com/micro/go-micro/v2/config/source/file"
)

type App struct {
	Name string
	PageSize  int
	PrefixUrl string
	RuntimeRootPath string

	ImageSavePath  string
	ImageMaxSize   int
	ImageAllowExts []string

	ExportSavePath string
	QrCodeSavePath string
	FontSavePath   string

	LogSavePath string
	LogSaveName string
	LogFileExt  string
	TimeFormat  string
}

var AppSetting = &App{}

type Server struct {
	RunMode      string
	HttpPort     int
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

var ServerSetting = &Server{}

type Database struct {
	Type        string
	User        string
	Password    string
	Host        string
	Name        string
	TablePrefix string
}

var DatabaseSetting = &Database{}

type Redis struct {
	Host        string
	Password    string
	MaxIdle     int
	MaxActive   int
	IdleTimeout time.Duration
}

var RedisSetting = &Redis{}


// Setup initialize the configuration instance
func Setup() {
	// load the config from a file source
	if err := config.Load(file.NewSource(
		file.WithPath("./config/app.yml"),
	)); err != nil {
		fmt.Println(err)
		return
	}

	config.Get("database").Scan(&DatabaseSetting)
	config.Get("app").Scan(&AppSetting)
	config.Get("server").Scan(&ServerSetting)
	config.Get("redis").Scan(&RedisSetting)
}
